package com.hfzy.ihk.web.linkcall.utils;

import com.hfzy.ihk.common.facade.enums.call.CommitTo;
import com.hfzy.ihk.common.support.redis.IdGenerator;
import com.hfzy.ihk.common.util.Utils;
import com.hfzy.ihk.common.util.uuid.base64uuid.UUIDs;

import com.hfzy.ihk.facade.linkcall.vo.task.ConnectWsVo;
import com.hfzy.ihk.facade.linkcall.vo.task.FreesWitchConnectWsVo;
import com.hfzy.ihk.facade.userCenter.entity.UserHasCallLine;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.facade.userCenter.enums.CallLineType;
import com.hfzy.ihk.facade.userCenter.enums.CallType;
import com.hfzy.ihk.web.oauth.client.utils.UserUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.stream.Collectors;

@Component
public class CallTaskUtils {

    @Value("${spring.profiles.active}")
    public String profile;

    public static String wsUrl;

    public static String freeswitchWsUrl;

    public static String freeswitchDissUrl;

    @Value("${cloudcall.ws-url}")
    public void setWsUrl(String wsUrl) {
        CallTaskUtils.wsUrl = wsUrl;
    }

    @Value("${freeswitch.wsUrl}")
    public void setFreeswitchWsUrl(String wsUrl) {
        CallTaskUtils.freeswitchWsUrl = wsUrl;
    }

    @Value("${freeswitch.dissUrl}")
    public void setFreeswitchDissUrl(String dissUrl) {
        CallTaskUtils.freeswitchDissUrl = dissUrl;
    }


    /**
     * 获取连接WS 的参数VO 类
     * */
    public static ConnectWsVo getConnectWsVo(String machineId,String pageFrom,String userId){
        ConnectWsVo connectWsVo = new ConnectWsVo();
        connectWsVo.setMachineId(machineId);
        connectWsVo.setPageFrom(pageFrom);
        connectWsVo.setUrl(wsUrl);
        connectWsVo.setUserId(userId);
        connectWsVo.setId(getWsConnParamsId());
        return connectWsVo;
    }


    /**
     * 获取 连接WS 的ID值
     * */
    public static String getWsConnParamsId(){
        return UUIDs.base64UUID()+"_"+ IdGenerator.generate("connectWs").toString();
    }

    /**
     * 获取FreesWitch连接参数 vo 类
     * */
    public static FreesWitchConnectWsVo getFreesWitchConnectWsVo(){
        FreesWitchConnectWsVo freesWitchConnectWsVo = new FreesWitchConnectWsVo();
        freesWitchConnectWsVo.setFreeswitchWsUrl(freeswitchWsUrl);
        freesWitchConnectWsVo.setFreeswitchDissUrl(freeswitchDissUrl);
        return freesWitchConnectWsVo;
    }

    // 将秒数格式化时间格式 时分秒
    public static String getForMatTime(Long connectTimeSecond){
        if(connectTimeSecond == null){
            return 0+"秒";
        }
        long hour = 0;
        long min = 0;
        long sec = 0;
        hour = (connectTimeSecond / (60 * 60) );
        min = ((connectTimeSecond / (60))  - hour * 60);
        sec = (connectTimeSecond-hour*60*60-min*60);
        if(hour != 0){
            return hour+"时"+min+"分"+sec+"秒";
        }else{
            if(min == 0){
                return sec+"秒";
            }else{
                return min+"分"+sec+"秒";
            }
        }
    }


    //获取用户绑定线路类型
    public static String getCommitTo(CallType callType){
        //获取当前登录用户
        UserInfo userInfo = UserUtil.getLoginUser();
        if(userInfo==null){
            throw new IllegalArgumentException("请先登录后再进行操作");
        }
        if(userInfo.getUserHasCallLines()==null && userInfo.getUserHasCallLines().isEmpty()){
            throw new IllegalArgumentException("该用户尚未绑定外呼线路");
        }
        UserHasCallLine userHasCallLine = null;
        userHasCallLine = Utils.getFirst(userInfo.getUserHasCallLines().stream().filter(it->it.getCallType()==callType).collect(Collectors.toList()));
        if(userHasCallLine==null){
            throw new IllegalArgumentException("该用户尚未配置该外呼类型的线路");
        }
        CallLineType callLineType = userHasCallLine.getCallLineType();
        CommitTo commitTo = null;
        switch (callLineType){
            case ALIYUN:
                commitTo = CommitTo.INT_ALIYUN;
                break;
            case FREESWITCHTEL:
                commitTo = CommitTo.FREESWITCH;
                break;
            case FREESWITCHPHONE:
                //todo 暂时未定
                commitTo = CommitTo.FREESWITCH;
                break;
            default:
                throw new IllegalArgumentException("系统尚未该外呼类型的线路信息 " + callLineType.getDesc());
        }
        return commitTo.getValue();
    }


}
